package com.education.controller.backend;

import com.aliyuncs.exceptions.ClientException;
import com.education.pojo.*;
import com.education.service.UserLoginService;
import com.education.utils.APPMD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 邓恒君
 * 个人中心系列功能Controller层
 * Created by Administrator on 2018/4/26.
 */
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
public class UserLoginController {
    @Autowired
    private UserLoginService userLoginService;

    private String code = "123456";

    private String text = "";

    //账号登录请求获取用户ID
    @RequestMapping("/loginUsername")
    public @ResponseBody
    User userLoginUsername(HttpServletRequest req){
        String username = req.getParameter("username");
        String pwd1 = req.getParameter("pwd");
        String pwd = APPMD5Util.getMD5(pwd1);
        int i = userLoginService.userCount(username);
        if(i>0){
            User user = userLoginService.userId(username,pwd);
            return user;
        }else{
            return null;
        }
    }

    //手机登录请求获取用户ID
    @RequestMapping("/loginPhone")
    public @ResponseBody
    User userLoginPhone(HttpServletRequest req){
        String phone = req.getParameter("phone");
        String yzm = req.getParameter("yzm");
        int i = userLoginService.getPhoneCount(phone);
        if(i>0&&code.equals(yzm)){
            User user = userLoginService.getUidPhone(phone);
            return user;
        }else{
            return null;
        }
    }


    //注册请求添加新用户
    @RequestMapping("/reg")
    public @ResponseBody
    int userReg(HttpServletRequest req) throws ClientException, InterruptedException {
        String username = req.getParameter("username");
        String password1 = req.getParameter("pwd");
        String password = APPMD5Util.getMD5(password1);
        String uQuestion = req.getParameter("uQuestion");
        String uAnswer = req.getParameter("uAnswer");
        String uPhone = req.getParameter("uPhone");
        String yzm = req.getParameter("yzm");
        int i = userLoginService.userCount(username);
        if(i>0){
            return 1;
        }else{
            if(!code.equals(yzm)){
                return 2;
            }else{
                User user = new User();
                user.setuUsername(username);
                user.setuPassword(password);
                user.setuQuestion(uQuestion);
                user.setuAnswer(uAnswer);
                String url = req.getSession().getServletContext().getRealPath("");
                user.setuHead(url+"/img/head/09.jpg");
                user.setuPhone(uPhone);
                user.setuCreateTime(new Date());
                userLoginService.addUser(user);
                return 0;
            }
        }
    }

    //获取账号对应密保问题
    @RequestMapping("/getQuestion")
    public @ResponseBody
    User userQuestion(HttpServletRequest req){
        String username = req.getParameter("username");
        int i = userLoginService.userCount(username);
        if(i>0){
            User user = userLoginService.userAnswer(username);
            return user;
        }else{
            return null;
        }
    }

    //根据密保修改密码
    @RequestMapping("/changeAnswer")
    public @ResponseBody
    int userAnswer(HttpServletRequest req){
        String username = req.getParameter("username");
        String password1 = req.getParameter("pwd");
        String password = APPMD5Util.getMD5(password1);
        String answer = req.getParameter("uAnswer");
        User user = new User();
        user.setuUsername(username);
        user.setuPassword(password);
        user.setuAnswer(answer);
        int i = userLoginService.userPasswordA(user);
        return i;
    }

    //根据原密码修改密码
    @RequestMapping("/passwordChange")
    public @ResponseBody
    int userPassword(HttpServletRequest req){
        String username = req.getParameter("username");
        String password1 = req.getParameter("pwd");
        String password = APPMD5Util.getMD5(password1);
        String pwdold1 = req.getParameter("pwdold");
        String pwdold = APPMD5Util.getMD5(pwdold1);
        Map map = new HashMap();
        map.put("uPassword",password);
        map.put("uUsername",username);
        map.put("uPasswordOld",pwdold);
        int i = userLoginService.userPasswordP(map);
        return i;
    }

    //根据用户ID获取用户信息
    @RequestMapping("/getUser")
    public @ResponseBody
    Map getUser(HttpServletRequest req){
        String uerid = req.getParameter("uid");
        int uid = Integer.parseInt(uerid);
        User user = userLoginService.getUser(uid);
        Date date = user.getuCreateTime();
        SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd hh时mm分ss秒");
        String date2 = myFmt.format(date);
        Map map = new HashMap();
        map.put("user",user);
        map.put("u_create_time",date2);
        System.out.print(map);
        return map;
    }

    //根据用户ID修改用户信息
    @RequestMapping("/updateUser")
    public @ResponseBody
    int updateUser(HttpServletRequest req) {
        String userid = req.getParameter("uid");
        int uid = Integer.parseInt(userid);
        String uhead = req.getParameter("uhead");
        String uname = req.getParameter("uname");
        String userage = req.getParameter("uage");
        int uage = 0;
        if(userage==""){
            uage=0;
        }else{
            uage = Integer.parseInt(userage);
        }
        String usex = req.getParameter("usex");
        String uaddress = req.getParameter("uaddress");
        User user = new User();
        user.setuId(uid);
        user.setuHead(uhead);
        user.setuName(uname);
        user.setuAge(uage);
        user.setuSex(usex);
        user.setuAddress(uaddress);
        user.setuUpdateTime(new Date());
        int i = userLoginService.updateUser(user);
        return i;
    }

    //根据用户ID修改用户头像
    @RequestMapping("/changeHead")
    public @ResponseBody
    int changeHead(HttpServletRequest req){
        String userid = req.getParameter("uid");
        int uid = Integer.parseInt(userid);
        String uhead = req.getParameter("uhead");
        int i = userLoginService.changeHead(uid,uhead);
        return i;
    }

    //短信验证登录界面
    @RequestMapping("/phoneMessageD")
    public @ResponseBody
    int phoneMessageD(HttpServletRequest req) throws ClientException, InterruptedException {
        String phone = req.getParameter("phone");
        int i = userLoginService.getPhoneCount(phone);
        if(i>0){
            code = PhoneMessage.sendMessage(phone);
            return 1;
        }else{
            return 0;
        }
    }

    //短信验证注册界面
    @RequestMapping("/phoneMessageZ")
    public @ResponseBody
    int phoneMessageZ(HttpServletRequest req) throws ClientException, InterruptedException {
        String phone = req.getParameter("uPhone");
        int i = userLoginService.getPhoneCount(phone);
        if(i>0){
            return 1;
        }else{
            code = PhoneMessage.sendMessage(phone);
            return 0;
        }
    }

    //根据用户ID获取个人收藏的试卷
    @RequestMapping("/getUserConllection")
    public @ResponseBody
    List<TestPaper> getUserCollection(HttpServletRequest req){
        String uid1 = req.getParameter("uid");
        int uid = Integer.parseInt(uid1);
        List<TestPaper> tp = userLoginService.getUserCollection(uid);
       return tp;
    }

    //根据用户ID和卷子名删除个人收藏
    @RequestMapping("/deleteUserCollection")
    public @ResponseBody
    int deleteUserCollection(HttpServletRequest req){
        String uid1 = req.getParameter("uid");
        int uid = Integer.parseInt(uid1);
        String tpname = req.getParameter("tpName");
       int i = userLoginService.deleteUserCollection(uid,tpname);
        return i;
    }

    //根据卷子名查询卷子ID
    @RequestMapping("/selectUserCollection")
    public @ResponseBody
    int selectUserCollection(HttpServletRequest req){
        String tpName = req.getParameter("tpName");
        int i = userLoginService.selectUserCollection(tpName);
        return i;
    }

    //获取头像
    @RequestMapping("/getImgHead")
    public @ResponseBody
    Map getImgHead(HttpServletRequest req){
        String userid = req.getParameter("uid");
        int uid = Integer.parseInt(userid);
        List<Img> list = userLoginService.getUserImg(uid);
//        String url = req.getRealPath("").split("target")[0];
        String url = req.getSession().getServletContext().getRealPath("");
        String[] head1 = ImgHead.getImgHead(url);
        Map map = new HashMap();
        map.put("head1",head1);
        map.put("head2",list);
        return map;
    }


    //上传图片
    @RequestMapping("/setImgHead")
    public @ResponseBody
    int setImgHead(HttpServletRequest req) throws IOException, SQLException {
        String userid = req.getParameter("uid");
        int uid = Integer.parseInt(userid);
        String imgentity = req.getParameter("img");
//        String url1 = req.getRealPath("").split("target")[0];
        String url2 = req.getSession().getServletContext().getRealPath("/");
//        String url3 = url2.substring(0,url2.lastIndexOf('\\')+1);
        String  src = ImgHead.setImgHead(imgentity,url2);
        String name = src.substring(src.lastIndexOf('\\')+1);
        Img img = new Img();
        img.setuId(uid);
        img.setImgName(name);
        img.setImgSrc(src);
        userLoginService.setUserHead(img);
        return 1;
    }

    //删除头像
    @RequestMapping("/deleteImgHead")
    public @ResponseBody
    int deleteImgHead(HttpServletRequest req) throws IOException, SQLException {
        String userid = req.getParameter("uid");
        int uid = Integer.parseInt(userid);
        String imgsrc = req.getParameter("imgsrc");
        String name = imgsrc.substring(imgsrc.lastIndexOf('/')+1);
        int i = userLoginService.deleteUserHead(uid,name);
        String url1 = req.getSession().getServletContext().getRealPath("/");
        ImgHead.deleteUserHead(name,url1);
        return i;
    }
}
